library(ggplot2)
library(gridExtra)
library(dplyr)

source("election_crimes1.R", encoding = "UTF-8")

years <- c(2008, 2012, 2014, 2016, 2020, 2022, 2024)

# Plot 1: law5 - People voting an absentee ballot intended for another person
law5_never_infrequent <- c(NA, 56, 62, 64, 63, 58, round(prop.table(svytable(~law5_3, national.design.climate.july24))[1] * 100))
law5_occasional_common <- c(NA, 44, 38, 36, 37, 42, round(prop.table(svytable(~law5_3, national.design.climate.july24))[2] * 100))

# Plot 2: law4 - People voting who are not U.S. citizens
law4_never_infrequent <- c(NA, 57, 61, 66, 63, 61, round(prop.table(svytable(~law4_3, national.design.climate.july24))[1] * 100))
law4_occasional_common <- c(NA, 43, 39, 34, 37, 39, round(prop.table(svytable(~law4_3, national.design.climate.july24))[2] * 100))

# Plot 3: law3 - People pretending to be someone else when going to vote
law3_never_infrequent <- c(71, 63, 69, 71, 70, 64, round(prop.table(svytable(~law3_3, national.design.climate.july24))[1] * 100))
law3_occasional_common <- c(29, 37, 31, 29, 30, 36, round(prop.table(svytable(~law3_3, national.design.climate.july24))[2] * 100))

# Plot 4: law1 - People voting more than once in an election
law1_never_infrequent <- c(NA, 66, 71, 73, 70, 66, round(prop.table(svytable(~law1_3, national.design.climate.july24))[1] * 100))
law1_occasional_common <- c(NA, 34, 29, 27, 30, 34, round(prop.table(svytable(~law1_3, national.design.climate.july24))[2] * 100))

# Plot 5: law6 - Officials changing the reported vote count in a way that is not a true reflection
law6_never_infrequent <- c(NA, 68, 72, 74, 69, 66, round(prop.table(svytable(~law6_3, national.design.climate.july24))[1] * 100))
law6_occasional_common <- c(NA, 32, 28, 26, 31, 34, round(prop.table(svytable(~law6_3, national.design.climate.july24))[2] * 100))

# Plot 6: law2 - People stealing or tampering with ballots that have been counted
law2_never_infrequent <- c(70, 67, 73, 74, 68, 66, round(prop.table(svytable(~law2_3, national.design.climate.july24))[1] * 100))
law2_occasional_common <- c(30, 33, 27, 26, 32, 34, round(prop.table(svytable(~law2_3, national.design.climate.july24))[2] * 100))

# Function to create a formatted plot
create_plot <- function(title, never_infrequent, occasional_common) {
  data <- data.frame(
    Year = rep(years, 2),
    Belief = rep(c("Not Common", "Common"), each = length(years)),
    Frequency = c(never_infrequent, occasional_common)
  )
  
  ggplot(data, aes(x = Year, y = Frequency, color = Belief, group = Belief)) +
    geom_line(size = 1.5) +
    geom_point(size = 4) +
    theme_minimal(base_size = 14) +
    ggtitle(title) +
    scale_y_continuous(limits = c(0, 100)) +
    scale_x_continuous(breaks = seq(2008, 2024, 2)) +
    geom_text(aes(label = ifelse(!is.na(Frequency), paste0(Frequency, "%"), "")), vjust = -1, size = 7.5, family = "Arial") +
    scale_color_manual(values = c("Not Common" = "#4ECDC4", "Common" = "#FF6B6B")) +
    theme(
      plot.title = element_text(size = 20, face = "bold", family = "Arial"),
      legend.position = "none",
      axis.title.x = element_blank(),
      axis.title.y = element_blank(),
      axis.text = element_text(size = 14, family = "Arial"),
      axis.line = element_line(color = "grey"),
      panel.grid.major = element_line(color = "grey90"),
      panel.grid.minor = element_line(color = "grey95"),
      plot.margin = unit(c(1, 1, 1, 1), "cm")
    ) +
    labs(y = "Percentage") +
    annotate("text", x = median(years, na.rm = TRUE), y = mean(never_infrequent, na.rm = TRUE) + 22, label = "Not Common", size = 7.5, color = "#4ECDC4", fontface = "bold", vjust = 0) +
    annotate("text", x = median(years, na.rm = TRUE), y = mean(occasional_common, na.rm = TRUE) - 22, label = "Common", size = 7.5, color = "#FF6B6B", fontface = "bold", vjust = -0)
}

# The rest of your code remains the same
plot1 <- create_plot("People voting an absentee ballot \nintended for another person", law5_never_infrequent, law5_occasional_common)
plot2 <- create_plot("People voting who are \nnot U.S. citizens", law4_never_infrequent, law4_occasional_common)
plot3 <- create_plot("People pretending to be someone else \nwhen going to vote", law3_never_infrequent, law3_occasional_common)
plot4 <- create_plot("People voting more than once \nin an election", law1_never_infrequent, law1_occasional_common)
plot5 <- create_plot("Officials fraudulently changing \nthe reported vote count", law6_never_infrequent, law6_occasional_common)
plot6 <- create_plot("People stealing or tampering \nwith ballots that have been voted", law2_never_infrequent, law2_occasional_common)

combined_plot <- grid.arrange(plot1, plot2, plot3, plot4, plot5, plot6, ncol = 3, nrow = 2, widths = c(1, 1, 1), heights = c(1, 1))
ggsave("combined_plots.png", combined_plot, width = 20, height = 16)

